<?php

namespace app\api\services\store;

use app\api\constant\CommentConst;
use app\api\model\GoodsComment;
use app\api\services\BaseServices;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class GoodsCommentServices extends BaseServices
{
    /**
     * 获取商品相关评论
     * @param int $id
     * @return bool|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getCommentWithUserInfo(int $id)
    {
        return GoodsComment::with(['user' => function ($query) {
            $query->field('id,username,nickname,avatar');
        }])->where('store_goods_id', $id)->select();
    }

    public static function commentList(int $goodsId, int $userid, string $level = '',$page = 1, $limit =10)
    {
//        [$page, $limit] = self::getPageValue();
        $query = GoodsComment::where('store_goods_id', $goodsId)->where('user_id', $userid);


        if (!empty($level)) {
            switch ($level) {
                case CommentConst::POSITIVE:
                    $query->where('star', '>=', 4);
                    break;
                case CommentConst::NEUTRAL:
                    $query->where('star', '=', 3);
                    break;
                case CommentConst::NEGATIVE:
                    $query->whereBetween('star', [1, 2]);
                    break;
            }
        }

        $list = $query
            ->with(['user' => function ($query) {
                $query->field('id,username,nickname,avatar');
            }])
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);

        return $list;

    }
}